package com.phs.main.web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phs.main.common.constant.AjaxResult;
import com.phs.main.web.po.Visitor;
import com.phs.main.web.service.IVisitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 访客 前端控制器
 * </p>
 *
 * @author Trent
 * @since 2020-07-07
 */
@Api
@RestController
@RequestMapping("/visitor")
public class VisitorController {
	@Lazy
	@Autowired
	IVisitorService VisitorService;


	@ApiOperation(value = "添加访客", response = Visitor.class)
	@PostMapping("/addVisitor")
	public AjaxResult addVisitor(@RequestBody Visitor Visitor) {
		try {
			VisitorService.save(Visitor);
			return AjaxResult.success("添加成功");
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

	@ApiOperation(value = "删除", notes = "根据ID删除访客")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "访客ID", dataType = "Long", required = true),
	})
	@DeleteMapping("deleteVisitorById/{id}")
	public AjaxResult deleteVisitorById(@PathVariable long id) {
		try {
			if (VisitorService.removeById(id)) {
				return AjaxResult.success("已删除");
			} else {
				return AjaxResult.error("未删除");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("更新访客")
	@PutMapping("/updateVisitor")
	public AjaxResult updateVisitor(@RequestBody Visitor Visitor) {
		try {
			if (VisitorService.updateById(Visitor)) {
				return AjaxResult.success("更改成功");
			} else {
				return AjaxResult.error("更改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("查询所有访客")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "页码", dataType = "Long"),
			@ApiImplicitParam(name = "size", value = "单页条数", dataType = "Long")
	})
	@GetMapping("/findAll/{current}/{size}")
	public AjaxResult findAll(@PathVariable Long current, @PathVariable(required = false) Long size) {
		if (current == null) {
			current = 1L;
		}
		if (size == null) {
			size = 10L;
		}
		Page<Visitor> page = new Page<>(current - 1, size);

		return AjaxResult.success(VisitorService.page(page));
	}

}
